using SmartCommunity.WebApi.Services.System;
using Microsoft.AspNetCore.Mvc;

namespace SmartCommunity.WebApi.Controllers.System
{
    /// <summary>
    /// API接口：菜单
    /// </summary>
    [ApiController]
    [Route("api/system/[controller]")]
    public class MenuController : BaseController
    {
        #region 构造函数
        private readonly MenuService menuService;
        public MenuController(MenuService menuService)
        {
            this.menuService = menuService;
        }
        #endregion

        #region 查询导出 
        /// <summary>
        /// 查询菜单树
        /// </summary>
        [HttpGet("tree")]
        public async Task<List<MenuDto>> GetMenuTree([FromQuery] MenuQuery query)
        {
            return await menuService.GetMenuTree(query, CurrentUser);
        }

        /// <summary>
        /// 通过主键查询
        /// </summary>
        /// <param name="id">主键ID</param>
        [HttpGet("{id}")]
        public async Task<MenuDto> GetOne(int id)
        {
            return await menuService.FindMenuById(id);
        }

        /// <summary>
        /// 导出excel
        /// </summary>
        [HttpGet("export")]
        public async Task<ActionResult> ExportExcel([FromQuery] MenuQuery query)
        {
            var data = await menuService.GetMenuTree(query, CurrentUser);
            var exporter = new ExcelExporter();
            exporter.AddTitleRow(0, "菜单名称", "菜单类型", "菜单地址", "权限标识", "菜单图标", "排序号", "备注说明", "创建人", "创建时间");
            int rowIndex = 1;
            foreach (var dto in data)
            {
                rowIndex = AddMenuRow(exporter, dto, rowIndex);
            }
            var bytes = exporter.GetExcelData();
            return File(bytes, CONTENT_TYPE_EXCEL, $"菜单导出{DateTime.Now:yyyyMMddHHmmss}.xlsx");
        }

        private static int AddMenuRow(ExcelExporter exporter, MenuDto dto, int rowIndex)
        {
            exporter.AddDataRow(rowIndex++, 0, dto.MenuName, dto.MenuType, dto.MenuPath, dto.Permission, dto.Icon, dto.SortNo, dto.Remark, dto.CreatedBy, dto.CreatedTime);
            foreach (var c in dto.Children ?? [])
            {
                rowIndex = AddMenuRow(exporter, c, rowIndex);
            }
            return rowIndex;
        }
        #endregion

        #region 增 删 改
        /// <summary>
        /// 新增菜单
        /// </summary>
        [HttpPost]
        [Log("新增菜单", OperateType.Create, false, true)]
        public async Task<ActionResult<MenuDto>> Create([FromBody] MenuDto dto)
        {
            var menu = await menuService.CreateMenu(dto, CurrentUser);
            return CreatedAtAction("GetOne", new { id = menu.MenuId }, menu);
        }

        /// <summary>
        /// 修改菜单
        /// </summary>
        [HttpPut("{id}")]
        [Log("修改菜单", OperateType.Update)]
        public async Task<ActionResult> Update(int id, [FromBody] MenuDto dto)
        {
            if (id != dto.MenuId)
            {
                return WarningMessage("要更新的数据和ID不匹配");
            }
            await menuService.UpdateMenu(dto, CurrentUser);
            return NoContent();
        }

        /// <summary>
        /// 删除菜单
        /// </summary>
        [HttpDelete]
        [Log("删除菜单", OperateType.Delete)]
        public async Task<ActionResult> Delete([FromQuery] int[] ids)
        {
            if (ids.Length == 0)
            {
                return ErrorMessage("参数错误：没有接收到要删除的主键值");
            }
            await menuService.DeleteByIds(ids, CurrentUser);
            return NoContent();
        }
        #endregion
    }
}
